package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.Atributo;
import com.liwalo.entities.ContactoTienda;
import com.liwalo.entities.Producto;
import com.liwalo.entities.Tienda;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class Atributo.
 * @see com.liwalo.dao.Atributo
 * @author Hibernate Tools
 */
public class AtributoDAO {

	private static final Log log = LogFactory.getLog(AtributoDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new InitialContext()
					.lookup("SessionFactory");
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(Atributo transientInstance) {
		log.debug("persisting Atributo instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Atributo instance) {
		log.debug("attaching dirty Atributo instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Atributo instance) {
		log.debug("attaching clean Atributo instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Atributo persistentInstance) {
		log.debug("deleting Atributo instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Atributo merge(Atributo detachedInstance) {
		log.debug("merging Atributo instance");
		try {
			Atributo result = (Atributo) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Atributo findById(java.lang.Integer id) {
		log.debug("getting Atributo instance with id: " + id);
		try {
			Atributo instance = (Atributo) sessionFactory.getCurrentSession()
					.get(Atributo.class, id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Atributo> findByExample(Atributo instance) {
		log.debug("finding Atributo instance by example");
		try {
			List<Atributo> results = (List<Atributo>) sessionFactory
					.getCurrentSession()
					.createCriteria(Atributo.class)
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<Atributo> listadoAtributosPorProducto(Producto productovo) {
		log.debug("finding Atributo instance by example");
		try {
			List<Atributo> results = (List<Atributo>) sessionFactory
					.getCurrentSession()
					.createCriteria(Atributo.class , "atributo")
					.createCriteria("atributo.productoId" , "productoid")
					.add(Restrictions.eq("productoid.id", productovo.getId()))
				    .list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public Atributo findByIdProducto(Producto producto) {
		List result = null;
		Transaction tx = null;
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		try{
            
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(Atributo.class, "atributo")
            //.createCriteria("contactotienda.tiendaId" , "tiendaid")
            .add(Restrictions.eq("atributo.productoId",producto));
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return (Atributo)result.get(0);
            }



		} catch (RuntimeException re) {
			throw re;
		}


            return null;
  }
}
